package com.like.hospitalworld;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.like.dialog.ImgDialogFragment;
import com.like.dialog.OfflineDownloadDialogFragment;
import com.like.entity.EventWrapper;
import com.like.entity.GeneralResponse;
import com.like.entity.InfoDetailReq;
import com.like.entity.InfoDetailResp;
import com.like.entity.InfoListResp;
import com.like.entity.MeetingCourseListResp;
import com.like.likeutils.network.RetrofitCallback;
import com.like.network.InfoService;
import com.like.network.RetrofitUtil;
import com.like.utils.Constants;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import retrofit2.Call;
import retrofit2.Response;

@ContentView(R.layout.activity_document_detail)
public class DocumentDetailActivity extends BaseActivity {

    public static final int REQUEST_INFO_BASE = 100;
    public static final int REQUEST_COURSE_INFO = 101;

    @ViewInject(R.id.main_content)
    private WebView mContentWebView;
    @ViewInject(R.id.title)
    private TextView mTitle;

    private InfoService mInfoService;
    private InfoListResp.InformationBase mInfo;
    private MeetingCourseListResp.MeetingCourse mMeetingCourse;
    private InfoDetailResp mInfoDetailResp;
    private ImgDialogFragment mImgDialog;

    private OfflineDownloadDialogFragment mOfflineDownloadDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        mImgDialog = new ImgDialogFragment();
        initWebView();
        mInfoService = RetrofitUtil.getService(InfoService.class);
        getInfoDetail();
        mOfflineDownloadDialog = new OfflineDownloadDialogFragment();
    }

    private void addImgClickListener() {
        mContentWebView.loadUrl(Constants.IMG_CLICK_JS);
    }

    private void initWebView() {
        mContentWebView.getSettings().setJavaScriptEnabled(true);
        mContentWebView.addJavascriptInterface(new JavascriptInterface(), "imageListner");
        mContentWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                addImgClickListener();
            }
        });
    }

    public class JavascriptInterface {
        @android.webkit.JavascriptInterface
        public void openImage(String url) {
            mImgDialog.setImgUrl(url);
            mImgDialog.show(getSupportFragmentManager(), "img_dialog");
        }
    }

    @Event(R.id.offline_download_container)
    private void downloadClick(View v) {
        mOfflineDownloadDialog.show(getSupportFragmentManager(), "download");
    }

    @Subscribe(sticky = true)
    public void onGetStickyEvent(EventWrapper event) {
        if (EventWrapper.isMatch(event, getClass(), REQUEST_INFO_BASE)) {
            mInfo = (InfoListResp.InformationBase) event.data;
        }
        if (EventWrapper.isMatch(event, getClass(), REQUEST_COURSE_INFO)) {
            mMeetingCourse = (MeetingCourseListResp.MeetingCourse) event.data;
        }
    }

    private void getInfoDetail() {
        int infoId = 0;
        if (mInfo != null)
            infoId = mInfo.id;
        if (mMeetingCourse != null)
            infoId = mMeetingCourse.id;
        InfoDetailReq req = new InfoDetailReq(infoId, m.getLoginResp().userID);
        Call<GeneralResponse<InfoDetailResp>> call = mInfoService.getInfoDetail(Constants.getGUID(), m.getToken(), req);
        call.enqueue(new RetrofitCallback<GeneralResponse<InfoDetailResp>>() {
            @Override
            public void onResponse(Call<GeneralResponse<InfoDetailResp>> call, Response<GeneralResponse<InfoDetailResp>> response) {
                mInfoDetailResp = response.body().data;
                mTitle.setText(mInfoDetailResp.title);
                mContentWebView.loadDataWithBaseURL(null,
                        Constants.FONT_STYLE_START + mInfoDetailResp.textInfo
                                + Constants.FONT_STYLE_END, "text/html", "utf-8", null);
            }

            @Override
            public void onFailure(Call<GeneralResponse<InfoDetailResp>> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
